/* app01-window.c
 *
 * Copyright 2024 wan-iot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "app01-config.h"
#include "app01-window.h"

struct _App01Window
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
GtkTreeView *main_tree;
  
};

G_DEFINE_TYPE (App01Window, app01_window, GTK_TYPE_APPLICATION_WINDOW)

static void
app01_window_class_init (App01WindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/app01-window.ui");
  gtk_widget_class_bind_template_child (widget_class, App01Window, header_bar);
   gtk_widget_class_bind_template_child (widget_class, App01Window, main_tree);
}
GtkTreeView *list;
GtkWidget *pasteMi;
GtkWidget *deleteMi;
void init_tree(){
  GtkTreeIter row;
 // gtk_list_store_append(GTK_LIST_STORE (store),&row);
  //为这一行添加数据
  // gtk_tree_store_set(GTK_TREE_STORE(store), &iter,
       //                      col_name, text,
         //                      -1); 
  GtkTreeStore *store = GTK_TREE_STORE (gtk_tree_view_get_model(list));
  g_print("store=%p\n",store);
  gtk_tree_store_append(store,&row,NULL);

  gtk_tree_store_set(store,&row,0,1,1,"abc",2,12.5,-1);
  
  gtk_tree_store_append(store,&row,NULL);
  gtk_tree_store_set(store,&row,0,2,1,"小明",2,4.5,-1);
  gtk_tree_store_append(store,&row,NULL);
  gtk_tree_store_set(store,&row,0,3,1,"小王",2,2.5,-1);
  
  //g_object_unref(store);
}
GtkTreeModel *createGtkTreeModel(){
  
  GtkTreeStore *store;
  store = gtk_tree_store_new(3,G_TYPE_INT,G_TYPE_STRING,G_TYPE_FLOAT);
  return GTK_TREE_MODEL (store);
}

void paste_click(){
  g_print("paste..\n");
  
}

gboolean deal_mouse_press(GtkTreeView *view,GdkEventButton *event,gpointer data){
    g_return_val_if_fail(view != NULL, FALSE);
    g_return_val_if_fail(GTK_IS_MENU(data), FALSE);
    g_return_val_if_fail(event != NULL, FALSE);
  
  g_print("单击了\n");
  if(event->button==3){   
       // GdkEventButton *mouse = (GdkEventButton *) event;
        if (event->button == GDK_BUTTON_SECONDARY)
        {
            gtk_widget_show_all(GTK_WIDGET(data));
          //TODO 获取选中项的标识
          g_signal_connect (pasteMi, "button-press-event", G_CALLBACK(paste_click), NULL);
            gtk_menu_popup_at_pointer(GTK_MENU(data), NULL);
            //gtk_menu_popup(GTK_MENU(data), NULL, NULL, NULL, NULL, mouse->button, mouse->time);
            //gtk_menu_popup_at_widget()
            return TRUE;
        }
       
  } 
    return FALSE;
}


static void
app01_window_init (App01Window *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  
  
  list = self->main_tree;
  GtkCellRenderer *renderer; 
  renderer = gtk_cell_renderer_text_new(); 
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),-1,"id",renderer,"text",0,NULL);
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),-1,"商品名称",renderer,"text",1,NULL);
  
  gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),-1,"价格",renderer,"text",2,NULL);
  
  GtkWidget *menu = gtk_menu_new();
  pasteMi = gtk_menu_item_new_with_label("Paste");
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), pasteMi);
  
  
  
  deleteMi = gtk_menu_item_new_with_label("Delete");
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), deleteMi);
  g_signal_connect (self->main_tree, "button-press-event", G_CALLBACK(deal_mouse_press), menu);
  
  
  
 GtkTreeModel *store =  createGtkTreeModel ();
   
  //绑定model
  gtk_tree_view_set_model(GTK_TREE_VIEW (list),store);
 
  init_tree();
  
  g_print("ddd\n");
}
